package com.sky.service.impl;


import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.sky.constant.MessageConstant;
import com.sky.constant.StatusConstant;
import com.sky.context.BaseContext;
import com.sky.dto.SetmealDTO;
import com.sky.dto.SetmealPageQueryDTO;
import com.sky.entity.Setmeal;
import com.sky.entity.SetmealDish;
import com.sky.exception.SetmealEnableFailedException;
import com.sky.mapper.DishMapper;
import com.sky.mapper.SetmealDishMapper;
import com.sky.mapper.SetmealMapper;
import com.sky.result.PageResult;
import com.sky.service.SetmealService;
import com.sky.vo.DishItemVO;
import com.sky.vo.DishVO;
import com.sky.vo.SetmealVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

@Service
public class SetmealServiceImpl implements SetmealService {

    @Autowired
    private SetmealMapper setmealMapper;

    @Autowired
    private SetmealDishMapper setmealDishMapper;

    @Autowired
    private DishMapper dishMapper;

    /**
     * 根据分类ID查询套餐
     * @param categoryId
     * @return
     */
    @Override
    public List<Setmeal> getSetmeal(Long categoryId) {
        List<Setmeal> setmeal = setmealMapper.getSetmeal(categoryId);
        return setmeal;

    }

    /**
     * 根据套餐id查询菜品
     * @param setmealId
     * @return
     */
    @Override
    public List<DishItemVO> getSetmealDish(Long setmealId) {
        List<DishItemVO> dish = setmealMapper.getSetmealId(setmealId);
        return dish;
    }

    /**
     * 套餐分页查询
     *
     * @param setmealPageQueryDTO
     * @return
     */
    @Override
    public PageResult page(SetmealPageQueryDTO setmealPageQueryDTO) {
        PageHelper.startPage(setmealPageQueryDTO.getPage(), setmealPageQueryDTO.getPageSize());

        Page<SetmealVO> page = setmealMapper.page(setmealPageQueryDTO);

        return new PageResult(page.getTotal(), page.getResult());
    }

    /**
     * 添加套餐
     *
     * @param setmealDTO
     */
    @Override
    public void add(SetmealDTO setmealDTO) {
        Setmeal setmeal = new Setmeal();
        BeanUtils.copyProperties(setmealDTO, setmeal);
        setmeal.setUpdateTime(LocalDateTime.now());
        setmeal.setCreateTime(LocalDateTime.now());
        setmeal.setCreateUser(BaseContext.getCurrentId());
        setmeal.setUpdateUser(BaseContext.getCurrentId());
        setmealMapper.insert(setmeal);

        Long id = setmeal.getId();
        List<SetmealDish> setmealDishes = setmealDTO.getSetmealDishes();
        if (setmealDishes != null && setmealDishes.size() > 0) {
            setmealDishes.forEach(setmealDish ->
                    setmealDish.setSetmealId(id));
        }

        setmealDishMapper.addSetmealDish(setmealDishes);
    }

    /**
     * 根据id查询套餐
     *
     * @param id
     * @return
     */
    @Override
    public SetmealVO getById(Long id) {
        Setmeal setmeal = setmealMapper.getIdSetmeal(id);

        SetmealVO setmealVO = new SetmealVO();

        BeanUtils.copyProperties(setmeal, setmealVO);

        List<SetmealDish> setmealDishId = setmealDishMapper.getSetmealDishId(id);

        setmealVO.setSetmealDishes(setmealDishId);

        return setmealVO;
    }

    /**
     * 批量删除套餐
     *
     * @param ids
     */
    @Override
    public void delete(List<Long> ids) {
        for (Long id : ids) {
            Setmeal setmeal = setmealMapper.getIdSetmeal(id);
            if (setmeal.getStatus() == StatusConstant.ENABLE){
                throw new SetmealEnableFailedException(MessageConstant.SETMEAL_ON_SALE);
            }
            List<Long> longs = setmealDishMapper.addDishId(ids);
            for (Long aLong : longs) {
                setmealDishMapper.deleteSetmealDishId(aLong);
            }
            setmealMapper.deleteSetmeal(id);
        }
    }

    /**
     * 修改套餐
     *
     * @param setmealDTO
     */
    @Override
    public void update(SetmealDTO setmealDTO) {
        Setmeal setmeal = new Setmeal();
        BeanUtils.copyProperties(setmealDTO,setmeal);
        setmeal.setUpdateUser(BaseContext.getCurrentId());
        setmeal.setUpdateTime(LocalDateTime.now());
        setmealMapper.updeteSetmeal(setmeal);

        Long id = setmeal.getId();
        List<Long> setmealDish = setmealDishMapper.findSetmealDish(setmealDTO.getId());
        for (Long dish : setmealDish) {
            setmealDishMapper.deleteSetmealDishId(dish);
        }
        List<SetmealDish> setmealDishes = setmealDTO.getSetmealDishes();
        if (setmealDishes !=null && setmealDishes.size()>0){
            for (SetmealDish dish : setmealDishes) {
                dish.setSetmealId(id);
            }
        }

        setmealDishMapper.addSetmealDish(setmealDishes);
    }

    /**
     * 套餐起售停售
     *
     * @param status
     * @param id
     */
    @Override
    public void status(Integer status, Long id) {
        List<SetmealDish> setmealDishId = setmealDishMapper.getSetmealDishId(id);
        for (SetmealDish setmealDish : setmealDishId) {
            Long dishId = setmealDish.getDishId();
            DishVO dishVO = dishMapper.getById(dishId);
            if (dishVO.getStatus() == StatusConstant.DISABLE){
                throw  new SetmealEnableFailedException(MessageConstant.SETMEAL_ENABLE_FAILED);
            }
            Setmeal build = Setmeal.builder()
                    .id(id)
                    .status(status)
                    .build();
            setmealMapper.updeteSetmeal(build);
        }
    }
}
